package org.ym.basic.exceptions;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

/**
 * 业务型错误
 *
 * @author ym
 * @date 2023-02-21 22:37:24
 */
@Getter
public class BaseException extends RuntimeException {
    private static final long serialVersionUID = -2953685435094647672L;
    public static final String COLON_BLANK = ": ";
    /**
     * 前缀
     */
    private String prefix;
    /**
     * 错误码
     */
    private String code;

    public BaseException() {
        super();
    }

    public BaseException(String prefix, String code) {
        super();
        this.prefix = prefix;
        this.code = code;
    }

    public BaseException(String prefix, String code, String message) {
        super(message);
        this.prefix = prefix;
        this.code = code;
    }

    public BaseException(Throwable cause) {
        super(cause);
    }

    public BaseException(String prefix, String code, Throwable cause) {
        super(cause);
        this.prefix = prefix;
        this.code = code;
    }

    public BaseException(String prefix, String code, String message, Throwable cause) {
        super(message, cause);
        this.prefix = prefix;
        this.code = code;
    }

    public BaseException(String prefix, String code, String message, Throwable cause,
                         boolean enableSuppression,
                         boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
        this.prefix = prefix;
        this.code = code;
    }

    public String getShowCode(){
        String showCode;
        if(StringUtils.isNotBlank(prefix)){
            showCode = prefix;
        }else{
            showCode = "";
        }
        if(StringUtils.isNotBlank(code)){
            showCode += code;
        }
        return showCode;
    }

    @Override
    public String toString() {
        String s = getClass().getName();
        String message = getMessage();
        String showCode = getShowCode();
        if(StringUtils.isNotBlank(showCode)){
            s = s + COLON_BLANK + showCode;
        }
        return (message != null) ? (s + COLON_BLANK + message) : s;
    }

}
